package com.bnlr.lrparking.widget;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bnlr.lrparking.R;
import com.bnlr.lrparking.appui.sharevalue.ShareActivity;
import com.bnlr.lrparking.appui.login.VerificaActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.HomePopBean;

/**
 * @author : tian
 *         time   : 2018/03/22
 *         desc   :
 */

public class PresentShareDialog extends DialogFragment {
    private ImageView ivClose;
    private TextView tvRmb;
    private TextView tvValue;
    private ImageView ivCheck;
    private static final String PRESENT_SHARE_DIALOG = "present_share_dialog";
    private HomePopBean.DataBean bean;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialog);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.present_share_dialog, null);
        initView(view);
        builder.setView(view);
        Dialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    public static PresentShareDialog newInstance(HomePopBean.DataBean bean) {
        Bundle args = new Bundle();
        PresentShareDialog fragment = new PresentShareDialog();
        args.putParcelable(PRESENT_SHARE_DIALOG,bean);
        fragment.setArguments(args);
        return fragment;
    }
    private void initView(View view) {
        ivClose = (ImageView) view.findViewById(R.id.iv_present_dialog_close);
        tvRmb = (TextView) view.findViewById(R.id.tv_present_rmb);
        tvValue = (TextView) view.findViewById(R.id.tv_present_contribution);
        ivCheck = (ImageView) view.findViewById(R.id.iv_present_check);
        initListener();
    }

    private void initListener() {
        if (bean != null) {
            tvRmb.setText(bean.getMoney()+"");
            tvValue.setText("即"+bean.getShareValue()+"贡献值");
        }
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        ivCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!AppConfig.getInstance().getBoolean("isLogin",false)) {
                    startActivity(new Intent(getActivity(), VerificaActivity.class));
                    dismiss();
                    return;
                } else {
                    startActivity(new Intent(getActivity(),ShareActivity.class));
                    dismiss();
                }
            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        bean = bundle.getParcelable(PRESENT_SHARE_DIALOG);

    }
    /**
     * 弹窗
     * @param fragmentManager
     */
    public void showDialog(FragmentManager fragmentManager) {
        this.show(fragmentManager, "PresentShareDialog");
    }
}
